package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Autowired
    private WmChannelMapper wmChannelMapper;

    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * admin 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult findList(WmChannelDto dto) {

        //分页参数检查
        dto.checkParam();

        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = Wrappers.<WmChannel>lambdaQuery();

        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmChannel::getName,dto.getName());
        }
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        IPage page = new Page(dto.getPage(), dto.getSize());
        page = page(page, lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult saveChannel(WmChannelDto dto) {

        if(dto ==  null || StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getDescription()) ||  StringUtils.isBlank(dto.getStatus()+"")){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        Integer count = wmChannelMapper.selectCount(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, dto.getName()));

        if(count > 0){
           return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称已存在");
        }

        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto,wmChannel);
        save(wmChannel);
        return ResponseResult.okResult(wmChannel);
    }

    @Override
    public ResponseResult updateChannel(WmChannelDto dto) {

        if(dto ==  null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto,wmChannel);

        updateById(wmChannel);

        return ResponseResult.okResult(wmChannel.getId());
    }

    @Override
    public ResponseResult del(int id) {
        if(id <= 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = wmChannelMapper.selectOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, id));

        if(wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if(wmChannel.getStatus() != false){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"非禁用状态该频道不能删除");
        }

        removeById(id);
        return ResponseResult.okResult(id);
    }

}